# -*- coding: utf-8 -*-


from PyQt4.QtGui import *
from PyQt4.QtCore import *

from UI.ui_mainWindow import Ui_MainWindow

from modules.dbData import dbData, daily
from settings.settings import res

from UI.daily.pageDaily import dlgDaily


# UI zoom
MARGIN = 5

class mainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowMinMaxButtonsHint)
        self.setupUi(self)

        self.dbData = dbData(res.userPath)
#        d = daily()
#        d.category = 1
#        d.money = 100.00
#        d.datetime = '2015/07/20 22:27:11'
#        self.dbData.addDaily(d)

        # UI zoom
        self.isLeftPressed = False
        self.pLast = QPoint()   # Global pos
        self.curPos = 0 # Mouse flag
        self.isFullScreen = False
        self.rectNormal = self.geometry()

        self.initUI()

        self.btnClose.clicked.connect(self.slotBtnClose)
        self.btnMin.clicked.connect(self.slotBtnMin)
        self.btnMax.clicked.connect(self.slotBtnMax)
        self.btnNormal.clicked.connect(self.slotBtnNormal)

    def initUI(self):
        # Page daily
        self.dlgDaily = dlgDaily(self.pageDaily)
        self.stackMain.setCurrentWidget(self.pageDaily)

    def paintEvent(self, event):
        self.setStyleSheet('QMainWindow#MainWindow{border: 1px solid #777777;'
                            'background-color: #FFFFFF;}')

        self.btnClose.setStyleSheet('QPushButton#btnClose{border-image: url(imgs/close_normal.png);}'
                'QPushButton#btnClose::hover{border-image: url(imgs/close_hover.png);}'
                'QPushButton#btnClose::pressed{border-image: url(imgs/close_pressed.png);}')

        self.btnMin.setStyleSheet('QPushButton#btnMin{border-image: url(imgs/min_normal.png);}'
                'QPushButton#btnMin::hover{border-image: url(imgs/min_hover.png);}'
                'QPushButton#btnMin::pressed{border-image: url(imgs/min_pressed.png);}')

        self.btnMax.setStyleSheet('QPushButton#btnMax{border-image: url(imgs/max_normal.png);}'
                'QPushButton#btnMax::hover{border-image: url(imgs/max_hover.png);}'
                'QPushButton#btnMax::pressed{border-image: url(imgs/max_pressed.png);}')

        self.btnNormal.setStyleSheet('QPushButton#btnNormal{border-image: url(imgs/normal_normal.png);}'
                'QPushButton#btnNormal::hover{border-image: url(imgs/normal_hover.png);}'
                'QPushButton#btnNormal::pressed{border-image: url(imgs/normal_pressed.png);}')

        self.widBar.setStyleSheet('QWidget#widBar{background-color: #E6E6E6; border-bottom: 1px solid #C8C8C8;}')

        self.labTitle.setStyleSheet('QLabel#labTitle {font: 12pt;'
                                    'font-weight: bold; color: #666666;}')

    def resizeEvent(self, event):
        tWidth = 28
        tHeight = 20
        self.widBar.setGeometry(QRect(1, 1, self.width()-2, 60))
        self.btnClose.setGeometry(QRect(self.width()-2-tWidth, 0, tWidth, tHeight))
        self.btnNormal.setGeometry(QRect(self.btnClose.x()-tWidth, 0, tWidth, tHeight))
        self.btnMax.setGeometry(QRect(self.btnClose.x()-tWidth, 0, tWidth, tHeight))
        if self.isFullScreen:
            self.btnNormal.setVisible(False)
            self.btnMax.setVisible(True)
        else:
            self.btnNormal.setVisible(True)
            self.btnMax.setVisible(False)

        self.btnMin.setGeometry(QRect(self.btnMax.x()-tWidth, 1, tWidth, tHeight))


        # Resize page widget
        self.stackMain.setGeometry(0, 60, self.width(), self.height()-60)

        self.dlgDaily.setGeometry(0, 0, self.stackMain.width(), self.stackMain.height())

    def slotBtnClose(self):
        self.close()

    def slotBtnMin(self):
        self.setWindowState(Qt.WindowMinimized)

    def slotBtnNormal(self):
        rectScreen = self.getAvailableScreen()
        self.isFullScreen = True
        self.rectNormal = self.geometry()
        self.setGeometry(rectScreen)

    def slotBtnMax(self):
        self.isFullScreen = False
        self.setGeometry(self.rectNormal)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.isLeftPressed = True
            self.pLast = event.globalPos()
            self.curPosY = event.pos().y()
            self.curPos = self.mouseState(event.pos())
            if not self.isFullScreen:
                self.rectNormal = self.geometry()
            event.ignore()

    def mouseReleaseEvent(self, event):
        if self.isLeftPressed:
            self.isLeftPressed = False
            tY = self.getAvailableScreen().y()
            if self.y() < tY:
                self.rectNormal = self.geometry()
                self.rectNormal.setTop(tY)
                self.setGeometry(self.rectNormal)

    def mouseDoubleClickEvent(self, event):
        if event.pos().y() > 60:
            return

        if event.button() == Qt.LeftButton:
            if self.isFullScreen:
                self.setGeometry(self.rectNormal)
                self.isFullScreen = False
                self.btnNormal.setVisible(True)
                self.btnMax.setVisible(False)
            else:
                self.rectNormal = self.geometry()
                self.setGeometry(self.getAvailableScreen())
                self.isFullScreen = True
                self.btnNormal.setVisible(False)
                self.btnMax.setVisible(True)

    def eventFilter(self, source, event):
        if self.isFullScreen:
            return QMainWindow.eventFilter(self, source, event)

        if event.type() == QEvent.MouseMove:
            poss = self.mouseState(event.globalPos() - self.geometry().topLeft())
            self.setCursorType(poss)
            if self.isLeftPressed:
                ptemp = event.globalPos()
                ptemp = ptemp - self.pLast
                if self.curPos == 22: # Move widget
                    tRect = self.getAvailableScreen()
                    tCurY = QCursor.pos().y()
                    if tCurY <= tRect.y():
                        self.isFullScreen = True
                        self.setGeometry(self.getAvailableScreen())
                    elif tCurY >= (tRect.bottom() - 10):
                        # move to available screen bottom, min 10
                        pass
                    elif self.curPosY < 60:
                        ptemp = ptemp + self.pos()
                        self.move(ptemp)
                else:
                    globalPoint = event.globalPos()
                    wid = self.geometry()
                    tl = wid.topLeft()
                    rb = wid.bottomRight()
                    # Resize widget
                    if self.curPos==21: # left
                        if (rb.x() - globalPoint.x()) <= self.minimumWidth():
                            wid.setX(tl.x())
                        else:
                            wid.setX(globalPoint.x())
                    elif self.curPos==23: # right
                        wid.setRight(wid.right()+ptemp.x())
                    elif self.curPos==12: # top
                        if self.height() > self.minimumHeight():
                            wid.setTop(wid.top()+ptemp.y())
                        if self.height() == self.minimumHeight():
                            if ptemp.y() < 0:
                                wid.setTop(wid.top()+ptemp.y())
                    elif self.curPos==32: # bottom
                        wid.setBottom(wid.bottom()+ptemp.y())
                    elif self.curPos==11: # top left
                        if (rb.x() - globalPoint.x()) <= self.minimumWidth():
                            wid.setX(tl.x())
                        else:
                            wid.setX(globalPoint.x())
                        if (rb.y() - globalPoint.y()) <= self.minimumHeight():
                            wid.setY(tl.y())
                        else:
                            wid.setY(globalPoint.y())
                    elif self.curPos==13: # top right
                        wid.setTopRight(wid.topRight()+ptemp)
                    elif self.curPos==31: # bottom left
                        wid.setBottomLeft(wid.bottomLeft()+ptemp)
                    elif self.curPos==33: # bottom right
                        wid.setBottomRight(wid.bottomRight()+ptemp)

                    self.setGeometry(wid)

                self.pLast = event.globalPos()

            event.ignore()

        return QMainWindow.eventFilter(self, source, event)

    def setCursorType(self, flag):
        cursor = Qt.ArrowCursor
        if flag==11 or flag==33:
            cursor = Qt.SizeFDiagCursor
        elif flag==13 or flag==31:
            cursor = Qt.SizeBDiagCursor
        elif flag==21 or flag==23:
            cursor = Qt.SizeHorCursor
        elif flag==12 or flag==32:
            cursor=Qt.SizeVerCursor
        elif flag==22:
            pass
        else:
            pass

        self.setCursor(cursor)

    def mouseState(self, rect):
        flag = 1
        if rect.x() < MARGIN:
            flag = 1
        else:
            if rect.x() > self.width() - MARGIN:
                flag = 3
            else:
                flag = 2

        if rect.y() < MARGIN:
            return 10 + flag
        elif rect.y() > self.height() - MARGIN:
            return 30 + flag
        else:
            return 20 + flag

    def getAvailableScreen(self):
        return QApplication.desktop().availableGeometry()
